/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.huangp.framework;

/**
 *
 * @author Patrick Huang
 */
public class PageBasicImpl implements Page{
    //--------------------------------------------------------------------------
    // Static Properties
    //--------------------------------------------------------------------------

    //--------------------------------------------------------------------------
    // Properties
    //--------------------------------------------------------------------------
    /**
     * current start index.
     * typically a page will list items start from this index plus maxPerPage
     * number of next items available.
     */
    private long currentIndex;
    /**
     * maximum number of item per page
     */
    private int maxPerPage;
    /**
     * maximum index available for pagination
     */
    private long maxIndex;
    // -------------------------------------------------------------------------
    // Implementation Methods
    // -------------------------------------------------------------------------
    public PageBasicImpl(long currentIndex,int maxPerPage,long maxIndex){
        this.currentIndex = currentIndex;
        this.maxPerPage = maxPerPage;
        this.maxIndex = maxIndex;
    }
    
    public boolean isNextAvailable() {
        return currentIndex + maxPerPage - 1 < maxIndex;
    }

    public boolean isPreviousAvailable() {
        return currentIndex > 0;
    }

    public long getNextStartIndex() {
        if(this.isNextAvailable()){
            return currentIndex + maxPerPage;
        }
        else{
            return currentIndex;
        }
    }

    public long getPreviousStartIndex() {
        long prev = currentIndex - maxPerPage;
        return (prev >= 0) ? prev : 0;
    }

    public int getMaxPerPage() {
        return this.maxPerPage;
    }
    // -------------------------------------------------------------------------
    // Mutator Methods
    // -------------------------------------------------------------------------
}
